﻿using HeerCards.Info;
using Native.Sdk.Cqp;
using Native.Sdk.Cqp.EventArgs;
using PCRRPG.Code.EconomySystem;
using System;
using System.Text;

namespace Code
{
    public class Event_Checkin : IMessageEvent
    {

        //public static void Process(CQGroupMessageEventArgs e)
        //{
        //    //throw new NotImplementedException();
        //    if (AppMain.GobalSetting.enableCheckin)
        //    {
        //        if (AppMain.GobalSetting.CheckinMessage.Trim() != "")
        //        {
        //            //checkGroup

        //            var path = e.CQApi.AppDirectory + "saves\\" + e.FromGroup.Id.ToString() + "\\" + e.FromQQ.Id.ToString() + ".json";
        //            var playerinfo = HCPlayer.loadFromFile(path);
        //            StringBuilder sb = new StringBuilder();
        //            if (playerinfo.LastCheckinTime < DateTime.Now.Date)
        //            {
        //                //已经过了一天了
        //                //（这边到时候自定义一下吧）
        //                playerinfo.LastCheckinTime = DateTime.Now.Date;
        //                sb.AppendLine("{at}\n签到成功,抽卡次数+15");
        //                playerinfo.UseTimes += 15;
        //                //foreach (var item in App.GobalSetting.CheckinRewards)
        //                //{
        //                //    if (item.Min != 0 && item.Max != 0)
        //                //    {
        //                //        var count = 0;
        //                //        if (item.Min == item.Max || item.Max == 0)
        //                //        {
        //                //            count = item.Min;
        //                //        }
        //                //        else
        //                //        {
        //                //            count = new Random().Next(item.Min, item.Max);
        //                //        }
        //                //        playerinfo.addCount(item.Name, count);
        //                //        sb.AppendLine(item.Name + "*" + count);
        //                //    }
        //                //}
        //                sb.Append("日期：" + DateTime.Now);
        //            }
        //            else
        //            {
        //                sb.Append("您今天已经签到过了");
        //            }
        //            if (!AppMain.VipInfo.isVIP && new Random().NextDouble() < 0.1)
        //            {
        //                sb.Append("小广告(oﾟvﾟ)ノ：授权版去广告+更多功能");
        //            }
        //            HCPlayer.saveToFile(path, playerinfo);

        //            e.FromGroup.SendGroupMessage(sb.ToString().Replace("{at}", CQApi.CQCode_At(e.FromQQ).ToString()));
        //        }
        //    }
        //}
        public string Message {
            get {
                return AppMain.commandHelper.CheckinCommand;
            }
        }

        public void Process(CQPrivateMessageEventArgs e)
        {
            throw new NotImplementedException();
        }

        public void Process(CQGroupMessageEventArgs e)
        {

            var time = EconomyOperater.GetValue(e.FromQQ.Id, e.FromGroup.Id, "签到", "时间", "");
            if (time == "")
            {
                //还未签到
                EconomyOperater.WriteValue(e.FromQQ.Id, e.FromGroup.Id, "签到", "时间", DateTime.Today);
            }
            else
            {
                //检查时间
                DateTime date = DateTime.Parse(time);
                if (date.Date == DateTime.Today)
                {
                    //今天签过了
                    e.FromGroup.SendGroupMessage("您今天已经签过到了~");
                    return;
                }
                else
                {
                    EconomyOperater.WriteValue(e.FromQQ.Id, e.FromGroup.Id, "签到", "时间", DateTime.Today);



                }
            }

            //加钱~
            StringBuilder sb = new StringBuilder();
            AppMain.GobalSetting.CheckinRewards = AppMain.GobalSetting.CheckinRewards ?? new System.Collections.Generic.List<RewardInfo>();
            sb.AppendLine($"{e.FromQQ.CQCode_At()} 签到成功");
            foreach (var item in AppMain.GobalSetting.CheckinRewards)
            {
                var c = new Random().Next(item.Min, item.Max);
                EconomyOperater.AddMoney(e.FromQQ.Id, e.FromGroup.Id, item.Name,c );
                sb.AppendLine($"{item.Name} : {c}");
            }
            e.FromGroup.SendGroupMessage(sb.ToString());
            
        }
    }
}